#include <windows.h>

#include "CTrade.h"
#include "CConsole.h"
#include "CTradeContainer.h"

CTrade::CTrade() {
	m_id = 0;
	m_pChar1 = 0;
	m_pChar2 = 0;
	m_char1Accepted = false;
	m_char2Accepted = false;

	m_pTradeCont1 = new CTradeContainer();
	m_pTradeCont2 = new CTradeContainer();
}

CTrade::~CTrade() {
}

unsigned int CTrade::getId() {
	return m_id;
}

void CTrade::setId(unsigned int newId) {
	m_id = newId;
}

CChar * CTrade::getChar1() {
	return m_pChar1;
}

void CTrade::setChar1(CChar * schar) {
	if(schar != NULL)
		m_pChar1 = schar;
	else
		CConsole::outDebOnly("Cant set char 1. Character NULL.");
}

CChar * CTrade::getChar2() {
	return m_pChar2;
}

void CTrade::setChar2(CChar * schar) {
	if(schar != NULL)
		m_pChar2 = schar;
	else
		CConsole::outDebOnly("Cant set char 2. Character NULL.");
}

void CTrade::accepted(CChar * schar, bool accept) {
	if(schar == m_pChar1)
		m_char1Accepted = accept;
	else if(schar == m_pChar2)
		m_char2Accepted = accept;
	else
		CConsole::outDebOnly("Character %d not in trade",schar->getId());
}

bool CTrade::isAccepted(CChar * schar) {
	if(schar == m_pChar1)
		return m_char1Accepted;
	else if(schar == m_pChar2)
		return m_char2Accepted;
	else
		return false;
}

CTradeContainer * CTrade::getTradeCont1() {
	return m_pTradeCont1;
}

CTradeContainer * CTrade::getTradeCont2() {
	return m_pTradeCont2;
}

void CTrade::updateTradeCont1(unsigned char slotID, unsigned char invSlotID, unsigned short itemID, unsigned long quantity) {
	CTradeContainer * tradeCont = getTradeCont1();
	if(tradeCont != NULL) 
	{
		if (invSlotID == 0)
		{
			CConsole::outDebOnly("Adding %d to tradecontainer 1",quantity);
			tradeCont->setGil(quantity);
		}
		else
		{
			tradeCont->setItemID(itemID, slotID);
			tradeCont->setQuantity(quantity, slotID);
			tradeCont->setInvSlotID(invSlotID, slotID);
		}
	}
	else
		CConsole::outDebOnly("Trade Container 1 is null.");
}

void CTrade::updateTradeCont2(unsigned char slotID, unsigned char invSlotID, unsigned short itemID, unsigned long quantity) {
	CTradeContainer * tradeCont = getTradeCont2();
	if(tradeCont != NULL)
	{
		if (invSlotID == 0)
		{
			CConsole::outDebOnly("Adding %d to tradecontainer 2",quantity);
			tradeCont->setGil(quantity);
		}
		else
		{
			tradeCont->setItemID(itemID, slotID);
			tradeCont->setQuantity(quantity, slotID);
			tradeCont->setInvSlotID(invSlotID, slotID);
		}
	}
	else
		CConsole::outDebOnly("Trade Container 2 is null.");
}
